package com.iuie.ns.datum.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;

/**
 * 小工具
 * @author liujie
 * @date 2018-02-24 17:25:24
 */
@RequestMapping("datum/devWidgets")
@Controller
public class DevWidgetController {
	
	private static final String ENCOD_METHOD = "UTF-8";

	/**
	 * 小工具-index
	 * @param request
	 * @return
	 */
	@RequestMapping
	public String index(HttpServletRequest request){
		return "view-datum/development/widgets";
	}
	
    /**
     * md5转化
     * @param text
     * @return
     */
    @RequestMapping("toMd5")
    @ResponseBody
    public ResponseInfo toMd5(String text) {
    	String md5Text = StringUtils.MD5Encode(text);
        return ResponseInfo.successInfoByObject(md5Text);
    }
    
    /**
     * BASE64加密
     * @return
     */
    @RequestMapping("toBase64")
    @ResponseBody
    public ResponseInfo encodeBase64(String text){
    	try {
			String base64Text = new String(Base64.encodeBase64(text.getBytes(ENCOD_METHOD)), ENCOD_METHOD);
			return ResponseInfo.successInfoByObject(base64Text);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	return ResponseInfo.errorInfo("转化异常");
    }
    
    /**
     * base64解密
     * @param text
     * @return
     */
    @RequestMapping("fromBase64")
    @ResponseBody
    public ResponseInfo decryptBase64(String text){
    	try {
			String base64Text = new String(Base64.decodeBase64(text.getBytes(ENCOD_METHOD)), ENCOD_METHOD);
			return ResponseInfo.successInfoByObject(base64Text);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	return ResponseInfo.errorInfo("转化异常");
    }
    
	@RequestMapping("formatJson")
	@ResponseBody
	public ResponseInfo formatJson(String text) {
		
		String json = StringUtils.formatJson(text);
		return ResponseInfo.successInfoByObject(json);
	}
    
}
